#! /bin/bash
#
# Tubemaster - Additional Utility
#
# Copyright (C) 2007 Siegfried-Angel Gevatter Pujals <siggi.gevatter@gmail.com>
# Thanks to the guys on #bash (at irc.freenode.net) for their help!
#
# This bash script tries to get into the «gui-src» folder from whitin the current
# location, generates a Python class for all Qt4 files in XML format (.ui files)
# and puts them into the «src» directory, as ui_{base filename}.py.
#
# If you don't want this script to display information about all files it processes,
# run it either with «--silent» or «-s» as the first argument. However, this won't
# stop possible error messages from being displayed.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

if [[ `basename ~+` != gui-src ]];
then
	# If this command is called from outside the «gui-src» folder, try to enter it.
	if [[ -e gui-src ]]; then
		cd gui-src
	elif [[ -e ../gui-src ]]; then
		cd ../gui-src
	elif [[ -e ../../gui-src ]]; then
		cd ../../gui-src
	else
		# If this isn't possible, abort.
		echo 'Please enter either the main folder, «src» or «gui-src» itself before you run this command.'
		exit 0
	fi
fi

for file in *.ui
do
	# Get the base name of the current file.
	clearName=${file%.ui}
	
	if [[ "$1" != '--silent' ]] && [[ "$1" != '-s' ]]
	then
		# If «silent mode» wasn't used, print a line about the current file.
		echo "Generating a class for $file on «src/ui_${clearName}.py»..."
	fi
	
	# Generate a Python class for it.
	pyuic4 $file > ../src/ui_${clearName}.py
done

exit 1	# Work done.
